package com.lee.rabbitmq.exchange.topic;

import com.lee.rabbitmq.util.RabbitMqUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;

/**
 * 使用topic策略交换机发送消息
 *
 * @author Lee
 * @createDate 2021/12/15 9:26
 */
public class Send {
    public static void main(String[] args) {
        Connection connection = RabbitMqUtil.getConnection();
        Channel channel = null;
        try {
            channel = connection.createChannel();

            //声明交换机
            channel.exchangeDeclare("topicExchange","topic",true);

            String message = "topic测试消息！";
            /**
             * 发送消息到指定的队列
             * 参数1  为交换机名称
             * 参数2  为消息的RoutingKey如果这个消息的RoutingKey和某个队列与交换机绑定的RoutingKey一致,那么这个消息就会发送的指定的队列中
             * 注意:
             *   1、发送消息时必须确保交换机已经创建并且确保已经正确的绑定到了某个队列
             */
            channel.basicPublish("topicExchange","aa.bb.cc",null,message.getBytes("utf-8"));
            System.out.println("消息发送成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            RabbitMqUtil.close(connection,channel);
        }
    }
}
